/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.rf.richfitwheel.admin.sys.service.impl;

import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.math.Calculator;
import com.rf.richfitwheel.admin.sys.dao.CaptchaMapper;
import com.rf.richfitwheel.admin.sys.model.Captcha;
import com.rf.richfitwheel.admin.sys.service.SysCaptchaService;
import com.rf.richfitwheel.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Date;

/**
 * 验证码
 *
 * @since 2.0.0 2018-02-10
 */
@Service("sysCaptchaService1")
public class SysCaptchaServiceImpl implements SysCaptchaService {
    
    /**
     * 是否启用验证码验证，默认是true，即使用验证码验证
     */
    @Value("${richfitwheel.common.validate.captcha}")
    private boolean validateCaptcha;
    @Autowired
	private CaptchaMapper captchaMapper;

    @Override
    public BufferedImage getCaptcha(String uuid) throws Exception {
        if(StringUtils.isBlank(uuid)){
            throw new Exception("uuid不能为空");
        }
        //生成文字验证码
        //定义图形验证码的长、宽、验证码字符数、干扰圈圈个数
//        LineCaptcha captchaShear = CaptchaUtil.createLineCaptcha(180, 60, 6, 20);
//        CircleCaptcha captchaShear = CaptchaUtil.createCircleCaptcha(180, 60, 6, 20);
//        ShearCaptcha captchaShear = CaptchaUtil.createShearCaptcha(200, 60, 6, 2);
        ShearCaptcha captchaShear = new ShearCaptcha(200, 60, 6, 2);
//        GifCaptcha captchaShear = CaptchaUtil.createGifCaptcha(180, 60);
        captchaShear.setGenerator(new MathGenerator(1));
        captchaShear.setBackground(new Color(0, 112, 192));
        captchaShear.createCode();
        int calculateResult = (int) Calculator.conversion(captchaShear.getCode());
        String code = calculateResult + "";
//        String code = captchaShear.getCode();

        Captcha captcha = new Captcha();
        captcha.setUuid(uuid);
        captcha.setCode(code);
        //5分钟后过期
        captcha.setExpireTime(DateUtils.addDateMinutes(new Date(), 5));
        captchaMapper.insert(captcha);

        return captchaShear.getImage();
    }

    /**
     * 验证码验证
     */
    @Override
    public boolean validate(String uuid, String code) {
        if(!validateCaptcha){
            return true;
        }
    	Captcha captchaEntity = captchaMapper.selectByPrimaryKey(uuid);
//        SysCaptchaEntity captchaEntity = this.selectOne(new EntityWrapper<SysCaptchaEntity>().eq("uuid", uuid));
        if(captchaEntity == null){
            return false;
        }

        //删除验证码
        captchaMapper.deleteByPrimaryKey(uuid);

        if(captchaEntity.getCode().equalsIgnoreCase(code) && captchaEntity.getExpireTime().getTime() >= System.currentTimeMillis()){
            return true;
        }

        return false;
    }
}
